{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "from faker import Faker\n",
    "import random\n",
    "import csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "faker=Faker()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "faker.seed(\"2018Z8020661080\")\n",
    "random.seed(\"2018Z8020661080\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "def produceStudent(stu_id:int):\n",
    "    student=dict()\n",
    "    student[\"id\"]=\"2018Z80209{:05}\".format(stu_id)\n",
    "    student[\"name\"]=faker.first_name()\n",
    "    student[\"score\"]=round(random.gauss(70,20),1)\n",
    "    student[\"age\"]=round(random.randint(20,30))\n",
    "    return student"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "def produceStudents(num:int):\n",
    "    student_list=[]\n",
    "    for i in range(num):\n",
    "        student_list.append(produceStudent(i+1))\n",
    "    return student_list\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def produceStudentToCSV(fname:str,count:int,seed=None):\n",
    "    if seed is not None:\n",
    "        faker.seed(seed)\n",
    "        random.seed(seed)\n",
    "    with open(fname,\"w\",newline=\"\") as fp:\n",
    "        fieldnames=[\"id\",\"name\",\"score\",\"age\"]\n",
    "        writer=csv.DictWriter(fp,fieldnames=fieldnames)\n",
    "        writer.writeheader()\n",
    "        writer.writerows(produceStudents(count))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "produceStudentToCSV(\"stu2000-withhead.csv\",2000,\"2018Z8020661080\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "def produceStudentToJson(fname:str,count:int,seed=None):\n",
    "    if seed is not None:\n",
    "        faker.seed(seed)\n",
    "        random.seed(seed)\n",
    "    with open(fname,\"w\",newline=\"\") as fp:\n",
    "        for i in range(count):\n",
    "            d=produceStudent(i)\n",
    "            js=json.dumps(d)\n",
    "            fp.write(js)\n",
    "            fp.write(\"\\n\")\n",
    "#         fieldnames=[\"id\",\"name\",\"score\",\"age\"]\n",
    "#         writer=csv.DictWriter(fp,fieldnames=fieldnames)\n",
    "#         writer.writeheader()\n",
    "#         writer.writerows(produceStudents(count))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "produceStudentToJson(\"stu.json\",2000,\"2018Z8020661080\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
